﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IInject.Registration
{
	public class FuncRegistryContext : RegistryContext
	{
		public FuncRegistryContext(IContainer container, Func<IContainer, object> resolver) : base(container)
		{
			Resolver = resolver;
		}

		public override object Instance
		{
			get
			{
				if (Resolver != null)
				{
					var instance = Resolver(Container);

					if (Scope == Scope.Singleton)
					{
						base.Instance = instance;
					}

					return instance;
				}

				return base.Instance;
			}
			set
			{
				base.Instance = value;
			}
		}

		/// <summary>
		/// If set will use this delegate to determine the instance.
		/// </summary>
		public Func<IContainer, object> Resolver { get; set; }
	}
}
